<?php
/**
 *
 *
 */
class Yz_Property_OneToMany
    extends Yz_Property_ReferenceList
{

    protected $keyName;

    /** Required config property 'foreignKey' which is the name of
     *      the referenced property (! - not a db field) in the referenced model
     *  @param Yz_Model_Interface|null $model => null for 'compatible' __construct
     */
    public function __construct( Yz_Model_Interface $model, $config=null )
    {
        debug();
        parent::__construct( $model, $config );
        $keyName = $this->fetchConfig('foreignKey');
        if (! $keyName)
            throw Yz_Property_Exception_MissingConfigParameter::_new();
        if (! $model->offsetExists($keyName))
            throw Yz_Property_Exception_InvalidConfig::_new();
        $prop = $model->offsetGet($keyName);
        if (! ($prop instanceof Yz_Property_ForeignKey))
            throw Yz_Property_Exception_InvalidConfig::_new();
        $this->keyName = $keyName;
    }

    public function getKeyName()
    {
        return $this->keyName;
    }


    /** Select a default value for an item, based on the property name
     *  @param string $propName
     *  @param Yz_Item_Interface $item
     *  @return default value
     *  @throw Yz_Property_Exception
     */
    public function selectValue($propName, Yz_Item_Interface $item)
    {
        if (! $item->id)
            return null;
            //return new Yz_List($this->model->getItemClass());
        return $this->model->fetchListByKey($this->keyName, $item);
    }

}
